#include "stdafx.h"
#include "xml_tree.h"
#include "canvas.h"
using namespace ui;

void XmlTreeNode::OnClickItem()
{
	return;
}

void XmlTreeNode::SetExpand(bool bExpand)
{
	std::wstring bkimage = bExpand ? L"treenode_open_16x16.png" : L"treenode_close_16x16.png";
	FindSubControl(L"icon")->SetBkImage(bkimage);
	__super::SetExpand(bExpand);
}


//----------------------------------------------------------------------------------------------
//-------------------------------------------XmlTree--------------------------------------------
//----------------------------------------------------------------------------------------------
XmlTree::XmlTree() : TreeView()
{
}


XmlTree::~XmlTree()
{
}

void XmlTree::InitTree(Canvas *canvas)
{
	m_pCanvas = canvas;
}

TreeNode *XmlTree::FindNodeByDataId(std::string id)
{
	TreeNode *root = m_pRootBox;
	TreeNode *nood = FindNodeByDataId(root, id);
	return nood;
}

TreeNode *XmlTree::FindNodeByDataId(TreeNode *node, std::string id)
{
	if (!node)
		return nullptr;
	if (node->GetUTF8DataID() == id)
		return node;
	TreeNode *findNode = nullptr;
	int size = node->GetChildNodeCount();
	for (int i = 0; i < size; i++)
	{
		TreeNode *childNode = node->GetChildNode(i);
		findNode = FindNodeByDataId(childNode, id);
		if (findNode)
			break;
	}
	return findNode;
}

bool XmlTree::InsertControl(Box* box, Control *control)
{
	bool ret = false;
	std::string boxId = box ? box->GetUTF8DataID() : "";
	TreeNode *boxNode = FindNodeByDataId(boxId);
	if (!boxNode)
	{
		assert(!m_pRootBox);
		TreeNode *root = GetRootNode();

		TreeNode *node = new XmlTreeNode;
		ui::GlobalManager::FillBoxWithCache(node, L"plant_tree/xml_tree_node.xml");
		std::wstring sClassName = control->GetControlClassName();
		std::wstring sName = control->GetName();
		std::wstring sContent;
		if (sName.empty())
			sContent = L"<" + sClassName + L" " + sContent + L">";
		else
			sContent = L"<" + sClassName + L"name=";
		static_cast<Label*>(node->FindSubControl(L"control"))->SetText(sContent);
		ret = root->AddChildNode(node);

		std::string id = control->GetUTF8DataID();
		assert(!id.empty());
		node->SetUTF8DataID(id);
		node->AttachSelect([this, id](ui::EventArgs* args){
			m_pCanvas->SelectControlById(id);
			return true;
		});
		if (dynamic_cast<Box*>(control)){
			node->AttachDoubleClick([this, node](ui::EventArgs* args){
				node->SetExpand(!node->IsExpand());
				return true;
			});
		}
		else
			assert(0);

		m_pRootBox = node;
	}
	else
	{
		TreeNode *node = new XmlTreeNode;
		ui::GlobalManager::FillBoxWithCache(node, L"plant_tree/xml_tree_node.xml");
		std::wstring sClassName = control->GetControlClassName();
		std::wstring sName = control->GetName();
		std::wstring sContent;
		if (sName.empty())
			sContent = L"<" + sClassName + L" " + sContent + L">";
		else
			sContent = L"<" + sClassName + L"name=";
		static_cast<Label*>(node->FindSubControl(L"control"))->SetText(sContent);
		ret = boxNode->AddChildNode(node);
		std::string id = control->GetUTF8DataID();
		assert(!id.empty());
		node->SetUTF8DataID(id);
		node->AttachSelect([this, id](ui::EventArgs* args){
			m_pCanvas->SelectControlById(id);
			return true;
		});
		if (dynamic_cast<Box*>(control)){
			node->AttachDoubleClick([this, node](ui::EventArgs* args){
				node->SetExpand(!node->IsExpand());
				return true;
			});
		}
		else
		{
			node->FindSubControl(L"icon")->SetBkImage(L"control.png");
		}
	}
	return ret;
}

